import Tips from './Tips';
import wepy from 'wepy';

///通用高德地图的API调用，这里直接调，不通过我们自己的API
export default class amapUtils {

    ///通过经纬度获取当前的区域信息
    static async getMapToAddress(lng,lat)
    {
        let key=wepy.$instance.globalData.AmapKey;
        let url="https://restapi.amap.com/v3/geocode/regeo"
        var data={
            key:key,
            location:lng+","+lat,
            radius:1000,
            extensions:"base",
            batch:false,
            roadlevel:0
        };
        var rs=await this.wxRequest("GET",url,data)
        return rs;
    }

   
    static async getAmapGeo(city,address)
    {
        let key=wepy.$instance.globalData.AmapKey;
        let url="https://restapi.amap.com/v3/geocode/geo"
        var data={
            key:key,
            address:address,
            city:city
        };
        var rs=await this.wxRequest("GET",url,data)
        return rs;
    }

    static async getAmapLocation(city,address)
    {
        let rs=await this.getAmapGeo(city,address).catch(ex=>{return {status:0,ex:ex,info:"解析地址信息出错了"}});
        return rs;
    }

 
    static async wxRequest(method, url, data) {
        const param = {
            url: url,
            method: method,
            data: data,
        };
       
        const wpRes = await wepy.request(param);
        let res = wpRes.data;
        if(res)
        {
            res.HttpCode=wpRes.statusCode;
            res.Next=true;
        }
        else
        {
            res={
                HttpCode:wpRes.statusCode,
                Code:wpRes.statusCode,
                Next:true
            };
        };
        return res;
    }

}